package com.wang.gallerybackend.api.aliyunai;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.wang.gallerybackend.api.aliyunai.model.CreateOutPaintingTaskRequest;
import com.wang.gallerybackend.api.aliyunai.model.CreateOutPaintingTaskResponse;
import com.wang.gallerybackend.api.aliyunai.model.GetOutPaintingTaskResponse;
import com.wang.gallerybackend.exception.BusinessException;
import com.wang.gallerybackend.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;


/**
 * 阿里云 AI 接口
 * author: wang
 */
@Slf4j
@Component
public class AliYunAiApi {

	// 读取配置文件
	@Value("${aliYunAi.apiKey}")
	private String apiKey;

	// 创建任务地址
	public static final String CREATE_OUT_PAINTING_TASK_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting";

	// 查询任务状态
	public static final String GET_OUT_PAINTING_TASK_URL = "https://dashscope.aliyuncs.com/api/v1/tasks/%s";


	/**
	 * 创建任务
	 *
	 * @param createOutPaintingTaskRequest 创建任务参数
	 * @return 创建任务结果
	 */
	public CreateOutPaintingTaskResponse createOutPaintingTask(CreateOutPaintingTaskRequest createOutPaintingTaskRequest) {
		if (createOutPaintingTaskRequest == null) {
			throw new BusinessException(ErrorCode.OPERATION_ERROR, "扩图参数为空");
		}
		System.err.println(apiKey);
		// 发送请求
		HttpRequest httpRequest = HttpRequest.post(CREATE_OUT_PAINTING_TASK_URL)
				.header("Authorization", "Bearer " + apiKey)
				// 必须开启异步处理
				.header("X-DashScope-Async", "enable")
				.header("Content-Type", "application/json")
				.body(JSONUtil.toJsonStr(createOutPaintingTaskRequest));
		// 处理响应
		try (HttpResponse httpResponse = httpRequest.execute()) {
			if (!httpResponse.isOk()) {
				log.error("请求异常：{}", httpResponse.body());
				throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 扩图失败");
			}
			CreateOutPaintingTaskResponse createOutPaintingTaskResponse = JSONUtil.toBean(httpResponse.body(), CreateOutPaintingTaskResponse.class);
			if (createOutPaintingTaskResponse.getCode() != null) {
				String errorMessage = createOutPaintingTaskResponse.getMessage();
				log.error("请求异常：{}", errorMessage);
				throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 扩图失败，" + errorMessage);
			}
			return createOutPaintingTaskResponse;
		}
	}

	/**
	 * 查询创建的任务结果
	 *
	 * @param taskId 任务 ID
	 * @return 任务结果
	 */
	public GetOutPaintingTaskResponse getOutPaintingTask(String taskId) {
		if (StrUtil.isBlank(taskId)) {
			throw new BusinessException(ErrorCode.OPERATION_ERROR, "任务 ID 不能为空");
		}
		// 处理响应
		String url = String.format(GET_OUT_PAINTING_TASK_URL, taskId);
		try (HttpResponse httpResponse = HttpRequest.get(url)
				.header("Authorization", "Bearer " + apiKey)
				.execute()) {
			if (!httpResponse.isOk()) {
				log.error("请求异常：{}", httpResponse.body());
				throw new BusinessException(ErrorCode.OPERATION_ERROR, "获取任务结果失败");
			}
			return JSONUtil.toBean(httpResponse.body(), GetOutPaintingTaskResponse.class);
		}
	}
}